package com.bqjr.cloud.zuul;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

@Component
public class ZuulRouteFilter extends ZuulFilter {
	
	private static Logger logger = LoggerFactory.getLogger(ZuulRouteFilter.class);
	
	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {
		RequestContext context = RequestContext.getCurrentContext();
		HttpServletRequest request = context.getRequest();
		
		/**
		logger.info("--------------" + request.getRequestURI() + " header start-----------------");
		Enumeration<String> enums = request.getHeaderNames();
		while(enums.hasMoreElements()) {
			String name = enums.nextElement();
			logger.info(name + ":" + request.getHeader(name));
		}
		logger.info("--------------header end-----------------");
		*/

		String serviceId = (String) context.get(FilterConstants.SERVICE_ID_KEY);
		if (serviceId != null) {
			String requestURI = request.getRequestURI();
			requestURI = requestURI.replace("/" + serviceId, "");
			context.set(FilterConstants.REQUEST_URI_KEY, requestURI);
		}
		return null;
	}
	
	@Override
	public String filterType() {
		return FilterConstants.ROUTE_TYPE;
	}

	@Override
	public int filterOrder() {
		return 0;
	}

}
